/*
 * Copyright (c) 2018. J4dream Created.
 */

package com.j4dream.property.leetcode.basic;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class MathBasic {
    public List<String> fizzBuzz(int n) {
        List<String> strs = new ArrayList<>();

        for (int i = 1; i <= n; i++) {
            String s;
            if (i % 3 == 0 && i % 5 == 0) {
                s = "FizzBuzz";
            } else if (i % 3 == 0) {
                s = "Fizz";
            } else if (i % 5 == 0) {
                s = "Buzz";
            } else {
                s = Integer.toString(n);
            }
            strs.add(s);
        }
        return strs;
    }

    public int countPrimes(int n) {
        boolean[] notPrime = new boolean[n];
        int count = 0;
        for (int i = 2; i < n; i++) {
            if (notPrime[i] == false) {
                count++;
                for (int j = 2; i * j < n; j++) {
                    notPrime[i * j] = true;
                }
            }
        }
        return count;
    }

    public boolean isPowerOfThree(int n) {
        if (n == 0) return false;
        while (n % 3 == 0) {
            n /= 3;
        }
        return n == 1;
    }

    private static int c2n(char c) {
        switch (c) {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            default:
                return 0;
        }
    }

    public static int romanToInt(String a) {
        int result = 0;
        for (int i = 0; i < a.length(); i++) {
            if (i > 0 && c2n(a.charAt(i)) > c2n(a.charAt(i - 1))) {
                // 先加了前面的数，所以减两倍
                result += c2n(a.charAt(i)) - 2 * c2n(a.charAt(i - 1));
            } else {
                result += c2n(a.charAt(i));
            }
        }
        return result;
    }

    public static void main(String[] args) {
        MathBasic mb = new MathBasic();
        log.info(mb.countPrimes(10) + "");

    }
}
